Skip to content

Move common caching class loading functionality to the separate class#10979

Merged
krisstern merged 1 commit intojenkinsci:masterfrom
dukhlov:improve-classloader-caching
Sep 2, 2025
Merged

Move common caching class loading functionality to the separate class#10979
krisstern merged 1 commit intojenkinsci:masterfrom
dukhlov:improve-classloader-caching

Conversation

@dukhlov
Copy link
Contributor

@dukhlov dukhlov commented Aug 16, 2025

Move common class-loading caching functionality into CachingClassLoader.

Update UberClassLoader to use CachingClassLoader, so now all results of loadClass() (not just findClass()) are cached.
This improves performance, especially because UberClassLoader uses a ExistenceCheckingClassLoader wrapper for the parent class loader, which introduces overhead.

By caching class-loading results for the parent as well, we avoid redundant lookups and reduce that overhead.

Testing done

Manual testing on local setup, jenkins ci gates.

Proposed changelog entries

  • Improve UberClassLoader class caching

Proposed changelog category

/label internal

Proposed upgrade guidelines

N/A

Submitter checklist

  • The Jira issue, if it exists, is well-described.
  • The changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developers, depending on the change) and are in the imperative mood (see examples). Fill in the Proposed upgrade guidelines section only if there are breaking changes or changes that may require extra steps from users during upgrade.
  • There is automated testing or an explanation as to why this change has no tests.
  • New public classes, fields, and methods are annotated with @Restricted or have @since TODO Javadocs, as appropriate.
  • New deprecations are annotated with @Deprecated(since = "TODO") or @Deprecated(forRemoval = true, since = "TODO"), if applicable.
  • New or substantially changed JavaScript is not defined inline and does not call eval to ease future introduction of Content Security Policy (CSP) directives (see documentation).
  • For dependency updates, there are links to external changelogs and, if possible, full differentials.
  • For new APIs and extension points, there is a link to at least one consumer.

Desired reviewers

@basil, @timja

Before the changes are marked as ready-for-merge:

Maintainer checklist

  • There are at least two (2) approvals for the pull request and no outstanding requests for change.
  • Conversations in the pull request are over, or it is explicit that a reviewer is not blocking the change.
  • Changelog entries in the pull request title and/or Proposed changelog entries are accurate, human-readable, and in the imperative mood.
  • Proper changelog labels are set so that the changelog can be generated automatically.
  • If the change needs additional upgrade steps from users, the upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the pull request title (see example).
  • If it would make sense to backport the change to LTS, a Jira issue must exist, be a Bug or Improvement, and be labeled as lts-candidate to be considered (see query).

@dukhlov dukhlov force-pushed the improve-classloader-caching branch 3 times, most recently from 495fb2f to a58340f Compare August 16, 2025 19:07
…er class

Use it for UberClassLoder, as a result - UberClassLoder caches all results of loadClass function
(not only findClass)
@dukhlov dukhlov force-pushed the improve-classloader-caching branch from a58340f to 5d9b429 Compare August 17, 2025 17:23
@dukhlov dukhlov marked this pull request as ready for review August 17, 2025 19:48
Copy link
Contributor

@jeromepochat jeromepochat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! This refactor makes total sense, encouraging single-responsibility principle. Thanks!
I just left minor suggestion about Optional usage, feel free to ignore!

Comment on lines +25 to +55
private final ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();

public CachingClassLoader(String name, ClassLoader parent) {
super(name, parent);
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Object classOrEmpty = cache.computeIfAbsent(name, key -> {
try {
return super.loadClass(name, false);
} catch (ClassNotFoundException e) {
// Not found.
return Optional.empty();
}
});

if (classOrEmpty == Optional.empty()) {
throw new ClassNotFoundException(name);
}

Class<?> clazz = (Class<?>) classOrEmpty;
if (resolve) {
resolveClass(clazz);
}
return clazz;
}

public void clearCacheMisses() {
cache.values().removeIf(v -> v == Optional.empty());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Maybe the using Optional as map value type could make its usage more natural. WDYT?

Suggested change
private final ConcurrentHashMap<String, Object> cache = new ConcurrentHashMap<>();
public CachingClassLoader(String name, ClassLoader parent) {
super(name, parent);
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Object classOrEmpty = cache.computeIfAbsent(name, key -> {
try {
return super.loadClass(name, false);
} catch (ClassNotFoundException e) {
// Not found.
return Optional.empty();
}
});
if (classOrEmpty == Optional.empty()) {
throw new ClassNotFoundException(name);
}
Class<?> clazz = (Class<?>) classOrEmpty;
if (resolve) {
resolveClass(clazz);
}
return clazz;
}
public void clearCacheMisses() {
cache.values().removeIf(v -> v == Optional.empty());
}
private final ConcurrentHashMap<String, Optional<Class<?>>> cache = new ConcurrentHashMap<>();
public CachingClassLoader(String name, ClassLoader parent) {
super(name, parent);
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Optional<Class<?>> classOrEmpty = cache.computeIfAbsent(name, key -> {
try {
return Optional.of(super.loadClass(name, false));
} catch (ClassNotFoundException e) {
// Not found.
return Optional.empty();
}
});
if (classOrEmpty.isEmpty()) {
throw new ClassNotFoundException(name);
}
Class<?> clazz = classOrEmpty.get();
if (resolve) {
resolveClass(clazz);
}
return clazz;
}
public void clearCacheMisses() {
cache.values().removeIf(Optional::isEmpty);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping with Optional consumes a bit more memory, so I decided not to use it. However, I agree it makes the code more natural. Let's wait for further reviews before making a final decision.

Copy link
Member

@timja timja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/label ready-for-merge


This PR is now ready for merge, after ~24 hours, we will merge it if there's no negative feedback.

Thanks!

@comment-ops-bot comment-ops-bot bot added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Sep 1, 2025
@krisstern krisstern merged commit 34345df into jenkinsci:master Sep 2, 2025
18 checks passed
@dukhlov dukhlov deleted the improve-classloader-caching branch September 4, 2025 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants